<template>
  <div class="mobile-menu-whole">
    <div class="mobile-menu">
      <div class="sidebar-button" :class="{ active: IsShowSidebar }" @click="handleMenu">菜单</div>
      <div class="toc-button" :class="{ active: IsShowToc }" @click="handleToc">目录</div>
    </div>

    <!-- 左侧抽屉 - 移动端侧边栏 -->
    <div class="drawer-overlay" v-if="IsShowSidebar" @click="handleMenu"></div>
    <div class="drawer drawer-left" :class="{ 'drawer-open': IsShowSidebar }">
      <div class="drawer-content">
        <SideBarComponent @close-drawer="handleMenu" />
      </div>
    </div>

    <!-- 右侧抽屉 - 移动端文章目录 -->
    <div class="drawer-overlay" v-if="IsShowToc" @click="handleToc"></div>
    <div class="drawer drawer-right" :class="{ 'drawer-open': IsShowToc }">
      <div class="drawer-content">
        <TocComponent @close-drawer="handleToc" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import SideBarComponent from './SideBarComponent.vue'
import TocComponent from './TocComponent.vue'

const IsShowSidebar = ref(false)
const IsShowToc = ref(false)

const handleMenu = () => {
  if (IsShowSidebar.value) {
    // 如果侧边栏已经打开，则关闭它
    IsShowSidebar.value = false
  } else {
    // 如果侧边栏关闭，则打开它并关闭目录
    IsShowSidebar.value = true
    IsShowToc.value = false
  }
}

const handleToc = () => {
  if (IsShowToc.value) {
    IsShowToc.value = false
  } else {
    IsShowToc.value = true
    IsShowSidebar.value = false
  }
}
</script>

<style scoped>
.mobile-menu-whole {
  background-color: #ffffff;
  width: 100%;
  height: 50px;
  border-bottom: 1px solid #e5e7eb;
  position: fixed;
  top: 70px;
  left: 0;
  z-index: 800;
  pointer-events: none;
}

.mobile-menu {
  padding: 0 10px;
  height: 50px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  max-width: 1200px;
  font-size: 1.5rem;
  /* 确保按钮可以点击 */
  pointer-events: auto;
}

.sidebar-button {
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #2c3e50;
  font-weight: 700;
  cursor: pointer;
  transition: color 0.2s ease;
}

.sidebar-button.active {
  color: #3a8ee6;
}

.toc-button {
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #2c3e50;
  font-weight: 700;
  cursor: pointer;
  transition: color 0.2s ease;
}

.toc-button.active {
  color: #3a8ee6;
}

/* 抽屉样式 */
.drawer-overlay {
  position: fixed;
  top: 120px;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 850;
  transition: opacity 0.3s ease;
}

.drawer {
  position: fixed;
  top: 120px;
  bottom: 0;
  width: 280px;
  background-color: #ffffff;
  z-index: 900;
  transition: transform 0.3s ease;
  overflow-y: auto;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.drawer-left {
  left: 0;
  transform: translateX(-100%);
}

.drawer-right {
  right: 0;
  transform: translateX(100%);
}

.drawer-open {
  transform: translateX(0);
}

.drawer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  border-bottom: 1px solid #e5e7eb;
}

.drawer-header h3 {
  margin: 0;
  font-size: 1.2rem;
  font-weight: 700;
  color: #2c3e50;
}

.close-button {
  font-size: 1.5rem;
  cursor: pointer;
  color: #2c3e50;
  transition: color 0.2s ease;
}

.close-button:hover {
  color: #3a8ee6;
}

.drawer-content {
  padding: 0;
  height: 100%;
  overflow-y: auto;
  pointer-events: auto;
}
</style>
